package com.lucaswangdev.logindemo.web.controller;

import com.lucaswangdev.logindemo.biz.request.concept.*;
import com.lucaswangdev.logindemo.biz.response.common.PagingResponse;
import com.lucaswangdev.logindemo.biz.service.ConceptService;
import com.lucaswangdev.logindemo.biz.service.UserService;
import com.lucaswangdev.logindemo.model.convert.ConceptConverter;
import com.lucaswangdev.logindemo.model.dto.ConceptDTO;
import com.lucaswangdev.logindemo.model.dto.UserDTO;
import com.lucaswangdev.logindemo.web.BaseResult;
import com.lucaswangdev.logindemo.web.vo.ConceptVO;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

@RestController
@RequestMapping("/concept")
public class ConceptController {
    @Resource
    private ConceptService conceptService;

    @Resource
    private UserService userService;

    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public BaseResult<Void> insert(@RequestBody ConceptCreateRequest request, HttpServletRequest httpServletRequest){
        UserDTO loginUser = userService.getLoginUser(httpServletRequest);
        request.setUserId(loginUser.getId());
        Boolean success = conceptService.create(request);
        if(success == null || !success) {
            return BaseResult.systemFail();
        }
        return BaseResult.success();
    }

    @RequestMapping(value = "/list-page", method = RequestMethod.POST)
    public BaseResult<PagingResponse<ConceptVO>> listPage(@RequestBody ConceptQueryRequest request, HttpServletRequest httpServletRequest){
        UserDTO loginUser = userService.getLoginUser(httpServletRequest);
        request.setUserId(loginUser.getId());
        PagingResponse<ConceptDTO> response = conceptService.listPage(request);
        if(response == null) {
            return BaseResult.systemFail();
        }
        if(response.isSuccess()){
            List<ConceptVO> vos = ConceptConverter.INSTANCE.convertDTOList2VOList(response.getTableData());
            PagingResponse<ConceptVO> result = PagingResponse.build(vos, response.getPaging());
            return BaseResult.success(result);
        } else {
            return BaseResult.bizFail(response.getErrorMsg());
        }
    }

    @RequestMapping(value = "/list-all", method = RequestMethod.POST)
    public BaseResult<List<Map<String, Object>>> listPage(@RequestBody ConceptAllQueryRequest request, HttpServletRequest httpServletRequest){
        UserDTO loginUser = userService.getLoginUser(httpServletRequest);
        request.setUserId(loginUser.getId());
        List<ConceptDTO> response = conceptService.listAll(request);
        if(response == null) {
            return BaseResult.systemFail();
        }
        List<ConceptVO> vos = ConceptConverter.INSTANCE.convertDTOList2VOList(response);
        List<Map<String, Object>> listValue = new ArrayList<>();
        List<ConceptDTO> conceptDTOS = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(vos)) {
            vos.forEach(conceptVO -> {
                Map<String, Object> item = new HashMap<>();
                item.put("value", conceptVO.getId());
                item.put("label", conceptVO.getName());
                listValue.add(item);
            });
        }
        return BaseResult.success(listValue);
    }

    @RequestMapping(value = "/names", method = RequestMethod.POST)
    public BaseResult<Map<Long, String>> findNamesByUserId(@RequestBody ConceptAllQueryRequest request, HttpServletRequest httpServletRequest){
        UserDTO loginUser = userService.getLoginUser(httpServletRequest);
        request.setUserId(loginUser.getId());
        List<ConceptDTO> response = conceptService.listAll(request);
        if(response == null) {
            return BaseResult.systemFail();
        }
        List<ConceptVO> vos = ConceptConverter.INSTANCE.convertDTOList2VOList(response);
        Map<Long, String> nameMap = new HashMap<>();
        if (CollectionUtils.isNotEmpty(vos)) {
            vos.forEach(conceptVO -> {
                nameMap.put(conceptVO.getId(),conceptVO.getName());
            });
        }
        return BaseResult.success(nameMap);
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public BaseResult<Void> update(@RequestBody ConceptUpdateRequest request, HttpServletRequest httpServletRequest){
        UserDTO loginUser = userService.getLoginUser(httpServletRequest);
        request.setUserId(loginUser.getId());
        Boolean success = conceptService.modified(request);
        if(success == null || !success) {
            return BaseResult.systemFail();
        }
        return BaseResult.success();
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public BaseResult<Void> delete(@RequestBody ConceptDeleteRequest request, HttpServletRequest httpServletRequest){
        UserDTO loginUser = userService.getLoginUser(httpServletRequest);
//        request.setUserId(loginUser.getId());
        Boolean success = conceptService.deleteById(request);
        if(success == null || !success) {
            return BaseResult.systemFail();
        }
        return BaseResult.success();
    }
}
